// i.d(t, { L: () => ObjectPool })
export default class ObjectPool<T = any> {
  comparer: any
  poolArray: { object: T; inUse: boolean }[]
  constructor(e?) {
    this.comparer = e || this.defaultComparer
    this.poolArray = []
  }
  add(e: T) {
    const t = this.createObjectDescriptor(e)
    t.object = e
    t.inUse = !0
    this.addObjectDescriptorToPool(t)
    return t
  }
  get(e?: T) {
    for (const t of this.poolArray) if (!t.inUse && this.comparer(t, e)) return (t.inUse = !0), t
    return null
  }
  free(e: T) {
    for (const t of this.poolArray) if (t.object === e) return (t.inUse = !1), !0
    return !1
  }
  all() {
    return this.poolArray
  }
  remove(e: T) {
    const t = this.poolArray.findIndex(t => t.object === e)
    return -1 !== t && (this.poolArray.splice(t, 1), !0)
  }
  defaultComparer(e, t) {
    return !0
  }
  createObjectDescriptor(e: T) {
    return { object: e, inUse: !1 }
  }
  addObjectDescriptorToPool(e: { object: T; inUse: boolean }) {
    this.poolArray.push(e)
  }
}
